package mesServices;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ConnecteurBD {
	
	// Établissement de la connexion
	private static String user = "root";
	private static String pwd = "";
	
	// Définition de l’url de connexion
	private static String url = "jdbc:mysql://localhost:3306/";
	private static String bd = "tp-boutique";
	private static Connection connect;
		
	// Connnection pour la BD
	public static boolean ouvrirConnection (){
		
		boolean isConnect =false;
		
		try {
			// Chargement du Driver
			Class.forName("com.mysql.jdbc.Driver");
			
			try {
				connect = DriverManager.getConnection(url+bd, user, pwd);
				
				isConnect = true;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return isConnect;
	}
	
	
	
	public static void fermerConnection(){
		
		try {
			connect.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public static PreparedStatement getPS(String query){
		PreparedStatement ps = null;
		
		ouvrirConnection();
		try {
			ps = connect.prepareStatement(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		return ps;
	}
}
